package com.issac.mmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.issac.mmall.entity.Cart;
import com.issac.mmall.entity.User;
import com.issac.mmall.entity.UserAddress;
import com.issac.mmall.mapper.CartMapper;
import com.issac.mmall.service.CartService;
import com.issac.mmall.service.UserAddressService;
import com.issac.mmall.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Issac
 * @since 2020-12-10
 */
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private UserAddressService userAddressService;

    @GetMapping("/add/{productId}/{price}/{quantity}")
    public String add(
            @PathVariable("productId") Integer productId,
            @PathVariable("price") Float price,
            @PathVariable("quantity") Integer quantity,
            HttpSession session
    ){
        Cart cart = new Cart();
        cart.setProductId(productId);
        cart.setCost(price*quantity);
        cart.setQuantity(quantity);
        User user = (User) session.getAttribute("user");
        cart.setUserId(user.getId());
        if (cartService.save(cart)){
            return "redirect:/cart/findAllCartList";
        }else {
            return "redirect:/productCategory/list";
        }
    }
    @GetMapping("/findAllCartList")
    public ModelAndView findAllCartList(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement1");
        User user = (User) session.getAttribute("user");
        if(user == null){
            modelAndView.addObject("cartList",new ArrayList<>());
        }else {
            modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }
    @GetMapping("/removeCart/{id}")
    public String removeCart(@PathVariable("id") Integer id){
        cartService.removeById(id);
        return "redirect:/cart/findAllCartList";
    }
    @GetMapping("/settlement2")
    public ModelAndView settlement2(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement2");
        User user = (User) session.getAttribute("user");
        if(user == null){
            modelAndView.addObject("cartList",new ArrayList<>());
        }else {
            modelAndView.addObject("userInfo",user);
            List<CartVO> allCartVOByUserId = cartService.findAllCartVOByUserId(user.getId());
            if (allCartVOByUserId.size()==0){
                modelAndView.setViewName("main");
            }
            modelAndView.addObject("cartList",allCartVOByUserId);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",user.getId());
            modelAndView.addObject("userAddress",userAddressService.list(queryWrapper));
        }
        return modelAndView;
    }
    @PostMapping("/updateCart/{id}/{quantity}/{cost}")
    @ResponseBody
    public String updateCart(
            @PathVariable("id") Integer id,
            @PathVariable("quantity") Integer quantity,
            @PathVariable("cost") Float cost
    ){
        Cart cart = cartService.getById(id);
        cart.setQuantity(quantity);
        cart.setCost(cost);
        if (cartService.updateById(cart)) {
            return "success";
        }
        return "filed";
    }
}

